import * as API from '../../../api/orderApi';
import * as util from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderId: '',
    pageNo: 1,
    pageSize: 20,
    order: {},
    dataSource: [],
    total: 0,
    hasRequest: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let orderId = options.orderId;
    let shardingId = options.shardingId;
    this.setData({
      orderId: orderId,
      shardingId: shardingId
    });

  },
  loadData: function (flag) {
    let postBody = {
      orderId: this.data.orderId,
      pageNo: this.data.pageNo,
      pageSize: 20
    };
    wx.showLoading({
      title: '正在加载数据'
    })
    this.setData({
      hasRequest: false
    })
    API.getContactsInOrderDetail(postBody).then(res => {
      if (!res)
        return;
      let list = [];
      if (flag)
        list = [];
      else
        list = this.data.dataSource;
      let lists = list.concat(res.contactManList);
      let token = wx.getStorageSync('token');
      this.setData({
        dataSource: lists,
        total: res.totalCount,
        order: res.order,
        hasRequest: true
      });
      wx.stopPullDownRefresh();
      wx.hideLoading();
      let postBody = {
        id: this.data.orderId,
        shardingId: this.data.order.shardingId,
        token: token
      }
      API.followIsRead(postBody).then(res => {}, err => {
        wx.showToast({
          title: '标记为已读失败',
          icon: 'none'
        })
      })
    }, err => {
      util.toast('读取数据失败，请稍后再试');
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadData(true);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadData(true);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var hasMore = this.data.dataSource.length < this.data.total;
    if (hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData();
    }
  }
})